//
//  HomeViewController.m
//  GenericProject
//
//  Created by 皮蛋菌 on 2023/5/6.
//  Copyright © 2023 漠然丶情到深处. All rights reserved.
//

#import "HomeViewController.h"
#import "VideoPlayerViewController.h"
#import "HomeTableViewCell.h"
#import "VideoModel.h"

@interface HomeViewController ()

@property(nonatomic,strong)NSMutableArray *videoListArray;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"视频播放器";
    // Do any additional setup after loading the view.
    [self analysis];
    [self createSubViews];
}

- (void)analysis {
    self.videoListArray = [NSMutableArray array];
    NSMutableArray *array = [NSMutableArray array];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSURL *documentsDirectoryURL = urls[0];

    NSError *error = nil;
    NSArray *fileURLs = [fileManager contentsOfDirectoryAtURL:documentsDirectoryURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:&error];

    if (error != nil) {
        NSLog(@"Error while enumerating files: %@", [error localizedDescription]);
    } else {
        for (NSURL *fileURL in fileURLs) {
            NSError *error = nil;
            NSDictionary *attributes = [fileManager attributesOfItemAtPath:[fileURL path] error:&error];
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            
            if (error != nil) {
                NSLog(@"Error while getting file attributes: %@", [error localizedDescription]);
            } else {
                NSNumber *fileSize = [attributes objectForKey:NSFileSize];
                double fileSizeInMB = [fileSize doubleValue] / (1024.0 * 1024.0);

                [dic setValue:fileURL forKey:@"videoUrlString"];
                [dic setValue:[[fileURL lastPathComponent] stringByDeletingPathExtension] forKey:@"videoNameString"];
                [dic setValue:[NSString stringWithFormat:@"%.2f MB",fileSizeInMB] forKey:@"videoSizeString"];
            }
            [array addObject:dic];
        }
    }
    
    NSArray *arr = [NSArray yy_modelArrayWithClass:[VideoModel class] json:array];
    [self.videoListArray addObjectsFromArray:arr];
    
}

- (void)createSubViews {
    [self.view addSubview:self.mainTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.videoListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell"];
    if (!cell) {
        cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomeTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    VideoModel *model = self.videoListArray[indexPath.row];
    cell.videoModel = model;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VideoModel *model = self.videoListArray[indexPath.row];
    
    VideoPlayerViewController *VideoPlayerVC = [[VideoPlayerViewController alloc]init];
    VideoPlayerVC.modalPresentationStyle = UIModalPresentationFullScreen;
    VideoPlayerVC.videoURL = [NSURL URLWithString:model.videoUrlString];
    [self presentViewController:VideoPlayerVC animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return LSSYRealValue(100);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
